import baseView from '../../../../app/base/baseView';

const { ccclass, property } = cc._decorator;

interface onShow { title?: string, duration?: number, success?: Function, fail?: Function, complete?: Function }

@ccclass
export default class roofToast extends baseView {

    @property(cc.Label)
    private labTip: cc.Label = null;
    @property(cc.Node)
    private content: cc.Node = null;

    @property(cc.Integer)
    private widthBorder: number = 20;

    @property(cc.Integer)
    private heightBorder: number = 0;

    private forceUpdateLabel(label) {
        label._updateRenderData ? label._updateRenderData(true) : label._forceUpdateRenderData(true);
    }

    private sizeChange() {
        this.content.width = this.labTip.node.width + this.widthBorder * 2;
        this.content.height = this.labTip.node.height + this.heightBorder * 2;
    }

    protected onLoad() {
        this.labTip.node.on('size-changed', this.sizeChange, this);
    }

    //显示信息
    protected onShow({ title = '', duration = 2000, success, fail, complete }: onShow = {}) {
        const maxLen = Math.ceil(cc.winSize.width * 2 / 3);
        this.node.stopAllActions();

        if (typeof title != 'string') {
            title = String(title);
        }

        this.labTip.overflow = cc.Label.Overflow.NONE;
        this.labTip.string = title;
        this.forceUpdateLabel(this.labTip);
        if (this.labTip.node.width > maxLen) {
            this.labTip.overflow = cc.Label.Overflow.RESIZE_HEIGHT;
            this.labTip.node.width = maxLen;
            this.forceUpdateLabel(this.labTip);
        }

        this.node.opacity = 255;
        var a1 = cc.delayTime(Math.floor(duration / 1000));
        var a2 = cc.fadeOut(1);
        var a3 = cc.callFunc(function () {
            this.node.active = false;
        }, this);
        var action = cc.sequence(a1, a2, a3);
        this.node.runAction(action);

        success && success();
        complete && complete();
    }
}
